package src.accenture.com.backing;

import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.myfaces.trinidad.context.RequestContext;
import src.accenture.com.Common.CommonUtil;
import src.accenture.com.session.SessionEJB;
import src.accenture.com.view.ParamObj;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import src.accenture.com.Common.GlobalConstants;
import src.accenture.com.entity.MblEmployee;
import src.accenture.com.view.RegistrationView;

public class Holidaydetail {

    private Context context = null;
    private SessionEJB sessionBean;
    private String empId;
    protected RequestContext currentRequestContext;
    private String msgDelete;
    private String msgSave;
    private MblEmployee userLogin;
    private RegistrationView registrationView;
    private String message;
    private boolean showMessage;
    
    public Holidaydetail() throws NamingException {
        context = new InitialContext();
        sessionBean = (SessionEJB)context.lookup("HolidaySessionEJB#src.accenture.com.session.SessionEJB");
        registrationView = this.getEntity();
        this.userLogin = CommonUtil.getMblEmployeeFromSession();
        currentRequestContext = RequestContext.getCurrentInstance();
        if (!FacesContext.getCurrentInstance().isPostback()) {
            this.setDataForEntity();
        }
    }

    private void setDataForEntity() {

        ParamObj obj = new ParamObj();
        obj = (ParamObj)this.getParam("Param");

        if (null != obj.getFromDate()) {
            this.getRegistrationView().setAddNew(false); //is update
            this.getRegistrationView().setEmpId(this.userLogin.getEmpId());
            this.getRegistrationView().setEmpName(this.userLogin.getEmpFirstname().concat(" ").concat(this.userLogin.getEmpLastname()));
            this.getRegistrationView().setHolidayTypeId(obj.getHolidayTypeId());
            this.getRegistrationView().setHolidayTypeName(obj.getHolidayTypeName());
            this.getRegistrationView().setFromTime(obj.getFromTime());
            this.getRegistrationView().setFromDate(obj.getFromDate());
            this.getRegistrationView().setNewFromDate(obj.getFromDate());
            this.getRegistrationView().setToTime(obj.getToTime());
            this.getRegistrationView().setToDate(obj.getToDate());
            this.getRegistrationView().setNewToDate(obj.getToDate());
            this.getRegistrationView().setReason(obj.getReason());

        } else {
            this.getRegistrationView().setAddNew(true); //is add new
            this.getRegistrationView().setEmpId(this.userLogin.getEmpId());
            this.getRegistrationView().setEmpName(this.userLogin.getEmpFirstname().concat(" ").concat(this.userLogin.getEmpLastname()));
            this.getRegistrationView().setHolidayTypeId(obj.getHolidayTypeId());
            this.getRegistrationView().setHolidayTypeName(obj.getHolidayTypeName());
            this.getRegistrationView().setFromTime(null);
            this.getRegistrationView().setNewFromDate(null);
            this.getRegistrationView().setToTime(null);
            this.getRegistrationView().setNewToDate(null);
            this.getRegistrationView().setReason(null);
        }
    }

    /**
     * Gets entity.
     * @return ClientChooseEntity
     */
    public RegistrationView getEntity() {
        return (RegistrationView)CommonUtil.getManagedBeanValue("view_registrationview");
    }

    public Object getParam(String paramKey) {
        Object result = currentRequestContext.getPageFlowScope().get(paramKey);
        currentRequestContext.getPageFlowScope().remove(paramKey);
        return result;
    }

    public void putParam(String paramKey, Object paramValue) {
        currentRequestContext.getPageFlowScope().put(paramKey, paramValue);
    }


    public void setSessionBean(SessionEJB sessionBean) {
        this.sessionBean = sessionBean;
    }

    public SessionEJB getSessionBean() {
        return sessionBean;
    }

    public void setEmpId(String empId) {
        this.empId = empId;
    }

    public String getEmpId() {
        return empId;
    }

    public String goListPage() {
        return "list";
    }

    public String saveAction() {
        String directPage = "detail";
        try {
            DateFormat dateFormat = new SimpleDateFormat(GlobalConstants.CONST_FORMAT_SHORT_DATE);
            String emp_id = this.getRegistrationView().getEmpId();
            String type_id = this.getRegistrationView().getHolidayTypeId();
            
            Date fromdate = null;
            
            if (null != this.getRegistrationView().getFromDate()){
                    fromdate = dateFormat.parse(this.getRegistrationView().getFromDate().trim());    
                }
            
            Date todate = null;
            if(null != this.getRegistrationView().getToDate()){
                    todate = dateFormat.parse(this.getRegistrationView().getToDate().trim());
                }
            
            Date newfromdate = dateFormat.parse(this.getRegistrationView().getNewFromDate().trim());
            Date newtodate = dateFormat.parse(this.getRegistrationView().getNewToDate().trim());

            //not approved status
            String approvalstatus = "0";
            Date approvaldate = null;
            String approvalby = null;
            String describe = null;

            String fromtime = "";
            if (null != this.getRegistrationView().getFromTime() && !this.getRegistrationView().getFromTime().trim().equals("")) {
                fromtime = this.getRegistrationView().getFromTime().trim();

            } else {
                fromtime = GlobalConstants.CONST_START_WORKTIME;
            }

            String totime = "";
            if (null != this.getRegistrationView().getToTime() && !this.getRegistrationView().getToTime().trim().equals("")) {
                totime = this.getRegistrationView().getToTime().trim();

            } else {
                totime = GlobalConstants.CONST_END_WORKTIME;
            }

            String reason = "";
            if (null != this.getRegistrationView().getReason() && !this.getRegistrationView().getReason().trim().equals("")) {
                reason = this.getRegistrationView().getReason();
            } else {
                reason = GlobalConstants.CONST_MESSAGE_NO_REASON;
            }
            int result = 0;
            if (this.getRegistrationView().isAddNew()) {
               result = sessionBean.addNewMblRegistration(emp_id, type_id, newfromdate, newtodate, approvalstatus,
                                                  approvaldate, approvalby, describe, fromtime, totime, reason);
            } else {
               result =
                    sessionBean.updateMblRegistration(emp_id, type_id, fromdate, todate, newfromdate, newtodate,
                                                      fromtime, totime, reason);
                if (result == 1) {
                    directPage = "list";
                }
            }
            
            if(result == 1){
               this.setMessage(GlobalConstants.CONST_MESSAGE_SUCCESS); 
               
            }else{
               this.setMessage(GlobalConstants.CONST_MESSAGE_FAIL); 
                }
           this.setShowMessage(true); 
            
        } catch (Exception e) {
            System.out.println("ERR :" + e.getMessage());
        }
        return directPage;
    }

    public String clearScreenAction() {
        this.getRegistrationView().setNewFromDate(null);
        this.getRegistrationView().setFromTime(null);
        this.getRegistrationView().setNewToDate(null);
        this.getRegistrationView().setToTime(null);
        this.getRegistrationView().setReason(null);
        this.setShowMessage(false);
        return "detail";
    }
   
    public String deleteAction() {
        String directPage = "detail";
        try {
            DateFormat dateFormat = new SimpleDateFormat(GlobalConstants.CONST_FORMAT_SHORT_DATE);
            String emp_id = this.getRegistrationView().getEmpId();
            String type_id = this.getRegistrationView().getHolidayTypeId();
            Date fromdate = dateFormat.parse(this.getRegistrationView().getFromDate().trim());
            Date todate = dateFormat.parse(this.getRegistrationView().getToDate().trim());

            int result = sessionBean.deleteMblRegistration(emp_id, type_id, fromdate, todate);
            if (result == 1) {
                directPage = "list";
            }
        } catch (Exception e) {
            System.out.println("ERR :" + e.getMessage());
        }
        return directPage;
    }

    public void setRegistrationView(RegistrationView registrationView) {
        this.registrationView = registrationView;
    }

    public RegistrationView getRegistrationView() {
        return registrationView;
    }

    public void setMsgDelete(String msgDelete) {
        this.msgDelete = msgDelete;
    }

    public String getMsgDelete() {
        return GlobalConstants.CONST_MESSAGE_CONFIRM_DELETE;
    }

    public void setMsgSave(String msgSave) {
        this.msgSave = msgSave;
    }

    public String getMsgSave() {
        return GlobalConstants.CONST_MESSAGE_CONFIRM_SAVE;
    }

    public void setUserLogin(MblEmployee userLogin) {
        this.userLogin = userLogin;
    }

    public MblEmployee getUserLogin() {
        return userLogin;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setShowMessage(boolean showMessage) {
        this.showMessage = showMessage;
    }

    public boolean isShowMessage() {
        return showMessage;
    }
}
